package com.youngball.exception;

import com.youngball.utill.Code;
import com.youngball.web.webconfig.BaseController;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**

 */
@ControllerAdvice
public class DefaultExceptionHandler extends BaseController {
    /**
     * 没有权限 异常
     * <p/>
     * 后续根据不同的需求定制即可
     * restful接口无权访问
     */
    @ExceptionHandler({UnauthorizedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Object processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e, HttpServletResponse response) {
        Map maps=new HashMap();
        maps.put("msg","你没有权限,请联系管理员");
        maps.put("code", Code.FORBID.toString());
        return new ResponseEntity<Map>(maps, responseHeaders, HttpStatus.FORBIDDEN);
    }
}
